// pages/mobile/mobile.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgpath: app.d.imgPath,
    mobile: '',
    code: '',
    send_code: '',
    but: true,
    zt: '获取验证',
    gettime: 60

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.d.path + 'My/myCard',
      method: "POST",
      data: {
        openid: app.d.openid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          mobile:res.data
        })
      },
      fail: function (e) {
        wx.showToast({
          title: '获取用户信息失败！',
          duration: 2000
        });
      },
    });
  },
  inputTyping: function (e) {
    this.setData({
      mobile: e.detail.value,
    })
  },
  inputCode: function (e) {
    this.setData({
      code: e.detail.value,
    })
  },
  but: function (e) {
    var that = this
    var mobile = that.data.mobile;
    var flag = that.checkMobile(mobile);
    if (flag) {
      that.data.gettime = 60;
      that.wait(that, that.data.gettime)
      wx.request({
        url: app.d.path + 'Index/sms',
        method: "POST",
        data: {
          num: mobile
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res);
          that.data.send_code = res.data.code;
        },
        fail: function (e) {
          wx.showToast({
            title: '获取用户信息失败！',
            duration: 2000
          });
        },
      });
    }
  },
  // 提交
  formSubmit: function (e) {
    var that = this;
    var mobile = that.data.mobile;
    
    if (mobile) {
      wx.showLoading({ title: '绑定中', })

      wx.request({
        url: app.d.path + 'My/updateCard',
        data: { "card": mobile, "openid": app.d.openid },
        method: "POST",
        header: {// 设置请求的 header
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          wx.hideLoading();
          if (res.data == 200) {
           
            wx.showModal({
              title: "提示",
              content: "绑定成功",
              success: function (res) {
                
                  wx.reLaunch({
                    url: '/pages/index/index'
                  })
                
              }
            });
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '绑定失败'
            });
            return false;
          }
        },
        fail: function () {
          // fail
          wx.showToast({
            title: '网络异常！',
            duration: 2000
          });
        },
      })
    }
  },

  checkMobile: function (mobile) {
    var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    var that = this
    if (!reg.exec(mobile)) {
      that.setData({
        msg: '手机号格式不正确'
      })
      return false;
    } else {
      that.setData({
        msg: ''
      })
      return true
    }
  },
  checkCode: function (code) {
    var that = this
    if (code != that.data.send_code || code == '') {
      that.setData({
        msg: '验证码不正确'
      })
      return false;
    } else {
      return true
    }
  },
  wait: function (that, time) {
    if (that.data.gettime <= 0) {
      that.data.gettime = 60;
      that.setData({
        but: 'but',
        gettime: "获取验证"
      })
      return
    }
    setTimeout(function () {
      that.setData({
        gettime: that.data.gettime - 1,
        but: '',
      })
      that.wait(that, that.data.gettime)
    }, 1000);


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})